﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Controls;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations;
using InfoPick.Lib.Entity;

namespace InfoPick.UI.ViewModel
{
    public class ServerModel : ViewModelBase
    {
        private string name;
        private string address;
        private int port = 23;
        private string userName;
        private string password;

        public Guid Guid { get; set; }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ValidationException("别名不能为空");
                }
                if (this.name != value)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");

                }
            }
        }

        public string Address
        {
            get
            {
                return this.address;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ValidationException("地址不能为空");
                }
                if (this.address != value)
                {
                    this.address = value;
                    this.OnPropertyChanged("Address");

                }
            }
        }

        public int Port
        {
            get
            {
                return this.port;
            }
            set
            {
                if (value == 0)
                {
                    throw new ValidationException("端口不能为0");
                }
                if (this.port != value)
                {
                    this.port = value;
                    this.OnPropertyChanged("Port");

                }
            }
        }

        public string UserName
        {
            get
            {
                return this.userName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ValidationException("用户名不能为空");
                }
                if (this.userName != value)
                {
                    this.userName = value;
                    this.OnPropertyChanged("UserName");

                }
            }
        }

        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ValidationException("密码不能为空");
                }
                if (this.password != value)
                {
                    this.password = value;
                    this.OnPropertyChanged("Password");

                }
            }
        }


        public static ServerModel FormServer(Server server)
        {
            return new ServerModel
            {
                Guid = server.Guid,
                Name = server.Name,
                Address = server.Address,
                Port = server.Port,
                UserName = server.UserName,
                Password = server.Password
            };
        }

        public Server ToServer()
        {
            ServerModel server = this;
            return new Server
            {
                Guid = server.Guid,
                Name = server.Name,
                Address = server.Address,
                Port = server.Port,
                UserName = server.UserName,
                Password = server.Password
            };
        }

        public ServerModel Copy()
        {
            ServerModel server = this;
            return new ServerModel
            {
                Guid = server.Guid,
                Name = server.Name,
                Address = server.Address,
                Port = server.Port,
                UserName = server.UserName,
                Password = server.Password
            };
        }

    }
}
